package com.lanwf.admin.sys.operate;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.lanwf.admin.BaseController;
import com.lanwf.IAdminIndexController;
import com.lanwf.admin.sys.PermissionService;
import com.lanwf.common.model.SysOperate;
import com.lanwf.admin.utils.Result;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Lanwf
 * @Date: 2021/12/24 10:55
 * @Description:
 */
@Path(value = "/admin/operate", viewPath = "/admin/sys/operate")
public class OperateAdminController extends BaseController implements IAdminIndexController {

    @Inject
    PermissionService permissionService;

    @Override
    public void index() {
        set("permission",permissionService.getPermission(getAccountId(),getInt(0)));
        render("index.html");
    }

    @Override
    public void paginate() {
        Integer p = getInt("pageNum", 1);
        Integer s = getInt("pageSize", 10);

        String orderByColumn = get("orderByColumn", "");
        String isAsc = get("isAsc", "");

        String name = get("name", "");

        List<Object> param = new ArrayList<>();
        String sql = " from sys_operate where 1=1 ";

        if (StrKit.notBlank(name)) {
            sql += " and name like ? or code like ? ";
            param.add("%" + name + "%");
            param.add("%" + name + "%");
        }

        sql += " order by ";

        if (StrKit.notBlank(orderByColumn)) {
            sql += "" + orderByColumn + " " + isAsc;
        }
        Page<SysOperate> paginate = SysOperate.dao.paginate(p, s, "select *", sql, param.toArray());
        renderJson(new Result<>(paginate.getTotalRow(), paginate.getList()));
    }

    @Override
    public void add() {
        render("add_edit.html");
    }

    @Override
    public void save() {
        getFile();
        SysOperate model = getModel(SysOperate.class, "operate");
        model.set("create_time", new Date()).save();
        renderJson(Ret.ok().set("msg", "添加成功"));
    }

    @Override
    public void edit() {
        Integer id = getInt(0);
        SysOperate account = SysOperate.dao.findById(id);

        set("operate", account);
        render("add_edit.html");
    }

    @Override
    public void update() {
        getFile();
        SysOperate model = getModel(SysOperate.class, "operate");
        model.update();
        renderJson(Ret.ok().set("msg", "修改成功"));
    }

    @Override
    public void delete() {
        Integer id = getInt(0);
        SysOperate.dao.deleteById(id);
        renderJson(Ret.ok().set("msg", "删除成功"));
    }

    /**
     * 名字去重判断
     */
    public void nameUnique() {
        Integer id = getInt("id");
        String name = get("name");

        SysOperate first = null;
        if (id != null) {
            first = SysOperate.dao.findFirst("select * from sys_operate where id != ? and name = ? ", id, name);
        } else {
            first = SysOperate.dao.findFirst("select * from sys_operate where name = ? ",name);
        }

        if (first != null) {
            renderJson(Ret.fail("msg", "名称重复"));
            return;
        }
        renderJson(Ret.ok("msg", "名称可用"));
    }

    /**
     * 编码去重判断
     */
    public void codeUnique() {
        Integer id = getInt("id");
        String code = get("code");

        SysOperate first = null;
        if (id != null) {
            first = SysOperate.dao.findFirst("select * from sys_operate where id != ? and code = ? ", id, code);
        } else {
            first = SysOperate.dao.findFirst("select * from sys_operate where code = ? ",code);
        }

        if (first != null) {
            renderJson(Ret.fail("msg", "编码重复"));
            return;
        }
        renderJson(Ret.ok("msg", "编码可用"));
    }
}

